package com.ruoyi.project.km.projectManager.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.km.projectManager.domain.KmPro;
import com.ruoyi.project.km.projectManager.service.ProManagerService;
import com.ruoyi.project.system.post.domain.Post;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/km/projectManager")
public class ProManagerController extends BaseController {
    //声明资源路径的公共路径段
    private String prefix = "km/projectManager";
    //声明Service层属性
    @Autowired
    private ProManagerService proManagerServiceImpl;

    //声明资源跳转单元方法:跳转到proMessage页面
    @RequiresPermissions("km:proMessage:view")
    @GetMapping("/proMessage")
    public String operlog()
    {
        return prefix + "/proMessage";
    }

    //声明单元方法:完成项目信息的分页加载
    @RequiresPermissions("km:projectManager:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(KmPro kmPro)
    {
        startPage();
        List<KmPro> list = proManagerServiceImpl.selectKmProList(kmPro);
        return getDataTable(list);
    }

    /**
     * 新增项目信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存岗位
     */
    @RequiresPermissions("km:proMessage:add")
    @Log(title = "新增项目信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated KmPro kmPro)
    {
        System.out.println("新增项目信息:"+kmPro);
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(proManagerServiceImpl.checkKmProNameUniqueService(kmPro)))
        {
            return error("新增项目'" + kmPro.getProName() + "'失败，项目名称已存在");
        }
        return toAjax(proManagerServiceImpl.insertKmPro(kmPro));
    }

    /**
     * 校验项目名称
     */
    @PostMapping("/checkKmProNameUnique")
    @ResponseBody
    public String checkPostNameUnique(KmPro kmPro)
    {
        return proManagerServiceImpl.checkKmProNameUniqueService(kmPro);
    }

    /**
     * 修改岗位
     */
    @GetMapping("/edit/{proId}")
    public String edit(@PathVariable("proId") Integer proId, ModelMap mmap)
    {
        mmap.put("kmPro", proManagerServiceImpl.selectKmProById(proId));
        return prefix + "/edit";
    }
    /**
     * 修改保存岗位
     */
    @RequiresPermissions("km:proMessage:edit")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated KmPro kmPro)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(proManagerServiceImpl.checkKmProNameUniqueService(kmPro)))
        {
            return error("修改项目信息'" + kmPro.getProName() + "'失败，项目名称已存在");
        }
        return toAjax(proManagerServiceImpl.updateKmPro(kmPro));
    }

    /**
     *
     */
    @RequiresPermissions("km:proMessage:remove")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(proManagerServiceImpl.deleteKmProByIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }
}
